package cn.gson.crm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.ChutiDao;
import cn.gson.crm.model.dao.ClassDao;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.dao.FollowUpDao;
import cn.gson.crm.model.dao.ProjectDao;
import cn.gson.crm.model.dao.ProjectLeaderDao;
import cn.gson.crm.model.dao.ProjectPlanDao;
import cn.gson.crm.model.dao.ResourcesDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.dao.TransferDao;
import cn.gson.crm.model.dao.UserDao;
import cn.gson.crm.model.dao.WorkJinDuDao;
import cn.gson.crm.model.entity.Chuti;
import cn.gson.crm.model.entity.Classes;
import cn.gson.crm.model.entity.Cuikuan;
import cn.gson.crm.model.entity.DaGang;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Emp;
import cn.gson.crm.model.entity.FollowUp;
import cn.gson.crm.model.entity.Project;
import cn.gson.crm.model.entity.ProjectLeader;
import cn.gson.crm.model.entity.ProjectMembers;
import cn.gson.crm.model.entity.ProjectPlan;
import cn.gson.crm.model.entity.Resources;
import cn.gson.crm.model.entity.Role;
import cn.gson.crm.model.entity.Sign;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.entity.StudnetClassShadow;
import cn.gson.crm.model.entity.Transfer;
import cn.gson.crm.model.entity.User;
import cn.gson.crm.model.entity.WhStudentJourney;
import cn.gson.crm.model.entity.WorkJinDu;
import cn.gson.crm.model.entity.WorkJournal;
import cn.gson.crm.model.entity.ZhangJie;
import cn.gson.crm.model.mapper.BedroomMapper;
import cn.gson.crm.model.mapper.ClassesMapper;
import cn.gson.crm.model.mapper.DeptMapper;
import cn.gson.crm.model.mapper.HeightSchoolTuitionMapper;
import cn.gson.crm.model.mapper.MaintenanceRecordMapper;
import cn.gson.crm.model.mapper.NewTechnologyMapper;
import cn.gson.crm.model.mapper.PaybackMapper;
import cn.gson.crm.model.mapper.PayrecordMapper;
import cn.gson.crm.model.mapper.ProjectMapper;
import cn.gson.crm.model.mapper.StudentJourneyMapper;
import cn.gson.crm.model.mapper.StudentMapper;
import cn.gson.crm.model.mapper.TeaemploymentMapper;
import cn.gson.crm.model.mapper.UniversityTuitionMapper;
import cn.gson.crm.model.mapper.WorkJinMapper;
import cn.gson.crm.service.finance.Menu;
import lombok.extern.log4j.Log4j;


@Controller
@RequestMapping("/")
@Log4j
public class AppController {
	

	@Autowired
	private PayrecordMapper recordMapper;
	
	@Autowired
	private PaybackMapper backMapper;
	
	@Autowired
	private HeightSchoolTuitionMapper hightMapper;
	
	@Autowired
	private ProjectMapper projectMapper;
	
	@Autowired
	private UniversityTuitionMapper uniMapper;
	
	@Autowired
	private DeptMapper empMapper;
	
	@Autowired
	private StudentMapper stuMapper;
	
	@Autowired
	private MaintenanceRecordMapper mrm;
	
	
	@Autowired
	private NewTechnologyMapper ntm;
	
	@Autowired
	private BedroomMapper brm;
	
	@Autowired
	private TransferDao transferDao;
	
	@Autowired
	private StudentDao stuDao;
	
	@Autowired
	private StudentJourneyMapper sjm;
	
	@Autowired
	private ProjectPlanDao planDao;
	
	@Autowired
	private ProjectLeaderDao leaderDao;
	/**
	 * 员工dao
	 */
	@Autowired
	private EmpDao empDao;
	@Autowired
	private ProjectDao projectDao;
	@Autowired
	private DeptMapper deptMapper;
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private ClassDao claDao;

	@Autowired
	private WorkJinDuDao jdDao;
	
	@Autowired
	private ChutiDao ctDao;
	
	@Autowired
	private WorkJinMapper jdMapper;
	
	@Autowired
	private TeaemploymentMapper teaMapper;
	

	public String index(){
		return "login";
	}
	
	@RequestMapping("/reject")
	public void reject(){
	}
	
	@RequestMapping("/index")
	public void zhujiem(){
	}
	@GetMapping("/index")
	public String xx(Model model,HttpSession session){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String today = df.format(new Date());// new Date()为获取当前系统时间
		//我的提醒
		User u = (User) session.getAttribute("user");
		int transfer = stuMapper.massageCount(u.getEmp().getId(), "进行中");
		model.addAttribute("t", transfer);
		
		//我的消息
		Long empTeacher=((User)session.getAttribute("user")).getEmp().getId();
		Long count=recordMapper.findCount(empTeacher);
		String job=((User)session.getAttribute("user")).getEmp().getJob();
		
		if(job.equals("班主任")) {
			model.addAttribute("count", count);
		}else {
			count=0l;
			model.addAttribute("count", count);
		}
		
		
		//今日签约人数
		int zscount = stuMapper.todayNewStudent();
		model.addAttribute("zscount", zscount);
		
		//今日缴款人数
		int jkcount = stuMapper.todayPaymentCount();
		model.addAttribute("jkcount", jkcount);
		
		//今日毕业人数
		int bycount = stuMapper.todayGraduateCount();
		model.addAttribute("bycount", bycount);
		
		//今日作业提交量
		int workcount = stuMapper.todayWorkJournalCount(today);
		model.addAttribute("workcount", workcount);
		
		Iterable<Project> project=projectDao.findAll();
		for(Project p:project) {
			String t=today.replaceAll("-", "");
			String d=p.getEndTime().replaceAll("-", "");
			int a=Integer.valueOf(t);
			int b=Integer.valueOf(d);
			if(today.equals(p.getEndTime()) || a>b) {
				p.setStatus("已完成");
				projectDao.save(p);
			}
		}
		
		return "index";		
	}
	
	/**
	 * 编辑表单
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("my_massage")
	public String open() {
		return "my_massage";
	}
	/**
	 * 催款信息的查询
	 */
	@RequestMapping("messagelist")
	@ResponseBody
	public Map<String,Object> list(Cuikuan cuikuan, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize,HttpSession session){
		
		//获取当前登录的员工id
		Long empTeacher=((User)session.getAttribute("user")).getEmp().getId();
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<Cuikuan> pageData = recordMapper.findAllCuiKuan(empTeacher);
		PageInfo<Cuikuan> info = new PageInfo<>(pageData);
		
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	@GetMapping("/login")
	public String login(@SessionAttribute(required = false, value = "user") User user) {
		if (user != null && user.getEmp()!=null) {
			// 已登录用户，返回login请求，则重定向回首页
			return "redirect:/index";
		}else if(user != null && user.getStudent()!=null) {
			return "redirect:/front_page";
		}
		return "login";
	}
	
	@Autowired
	private ResourcesDao resDao;
	/**
	 * 登录
	 * 
	 * @param account
	 *            账号
	 * @param password
	 *            密码
	 */
	@PostMapping("/login")
	public String doLogin(@RequestParam String account, 
			@RequestParam String password, 
			RedirectAttributes attributes, 
			HttpSession session,HttpSession session1) {
		User user = userDao.findByAccount(account);
		if (user != null && user.getPassword().equals(password)&&user.getEmp()!=null) {
			if(user.getEnable() == true) {
				 Set<Resources> permissions;
	             //判断是否是超级管理员
	             if (0 == user.getId()) {
	                 permissions = resDao.findAllByEnableOrderByWeightDesc(true);
	             } else {
	                 //获取用户菜单
	                 List<Role> roles = user.getRoles();
	                 permissions = new HashSet<>();
	                 roles.forEach(role -> permissions.addAll(role.getResources()));
	             }

	             //存储菜单
	             TreeSet<Resources> menus = new TreeSet<>((o1, o2) -> {
	                 if (Objects.equals(o1.getWeight(), o2.getWeight())) {
	                     return -1;
	                 }
	                 return o1.getWeight() - o2.getWeight();
	             });

	             //存储权限key
	             Set<String> keys = new HashSet<>();
	             //所有有权限访问的请求
	             Set<String> urls = new HashSet<>();

	             permissions.forEach(permission -> {
	                 if (permission.getEnable()) {
	                     if (permission.getType().equals(Resources.Type.MENU)) {
	                         //是菜单
	                         menus.add(permission);
	                         urls.add(permission.getPath());
	                     }
	                     //获取用户拥有的权限
	                     keys.add(permission.getPermissionKey());

	                     urls.addAll(Arrays.asList(permission.getResource().split(",")));
	                 }
	             });

	             //树形数据转换
	             List<Menu> menuList = new ArrayList<>();
	             menus.forEach(permission -> {
	                 Menu m = new Menu();
	                 m.setId(permission.getId());
	                 m.setText(permission.getName());
	                 m.setHref(permission.getPath());
	                 m.setParentId(permission.getParent() == null ? null : permission.getParent().getId());
	                 menuList.add(m);
	             });
	             List<Menu> xtmenuList = new ArrayList<>();
	             List<Menu> zsmenuList = new ArrayList<>();
	             List<Menu> cwmenuList = new ArrayList<>();
	             List<Menu> jxmenuList = new ArrayList<>();
	             List<Menu> jymenuList = new ArrayList<>();
	             List<Menu> gxmenuList = new ArrayList<>();
	             for(int i=0;i<menuList.size();i++) {
	            	 if(menuList.get(i).getParentId() == 1) {
	            		 xtmenuList.add(menuList.get(i));
	            	 }else if(menuList.get(i).getParentId() == 2) {
	            		 zsmenuList.add(menuList.get(i));
	            	 }else if(menuList.get(i).getParentId() == 3) {
	            		 cwmenuList.add(menuList.get(i));
	            	 }else if(menuList.get(i).getParentId() == 4) {
	            		 jxmenuList.add(menuList.get(i));
	            	 }else if(menuList.get(i).getParentId() == 5) {
	            		 jymenuList.add(menuList.get(i));
	            	 }else if(menuList.get(i).getParentId() == 6) {
	            		 gxmenuList.add(menuList.get(i));
	            	 }
	             }
	             session.setAttribute("user", user);
	             session.setAttribute("xtmenuList", xtmenuList);
	             session.setAttribute("zsmenuList", zsmenuList);
	             session.setAttribute("cwmenuList", cwmenuList);
	             session.setAttribute("jxmenuList", jxmenuList);
	             session.setAttribute("jymenuList", jymenuList);
	             session.setAttribute("gxmenuList", gxmenuList);
	             session.setAttribute("keys", keys);
	             session.setAttribute("urls", urls);
				 return "redirect:/index";
			}else {
				attributes.addFlashAttribute("error", "该用户已被封号，请联系管理解封");
				session.invalidate();
				return "redirect:/login";
			}
			
		} else if(user != null && user.getPassword().equals(password)&&user.getStudent()!=null){
			if(user.getEnable() == true) {
				session1.setAttribute("user", user);
				return "redirect:/front_page";
			}else {
				attributes.addFlashAttribute("error", "该用户已被封号，请联系管理解封");
				session.invalidate();
				return "redirect:/login";
			}
			
		}else {
			attributes.addFlashAttribute("error", "用户名或密码错误！");
			session.invalidate();
			return "redirect:/login";
		}
	}

	/**
	 * 系统管理二级菜单
	 * @param xtmenuList
	 * @return
	 */
    @RequestMapping("/xtmenuList")
    @ResponseBody
    public List<Menu> xtmenuList(@SessionAttribute("xtmenuList") List<Menu> xtmenuList) {
        return xtmenuList;
    }
    /**
     * 招生管理二级菜单
     * @param zsmenuList
     * @return
     */
    @RequestMapping("/zsmenuList")
    @ResponseBody
    public List<Menu> zsmenuList(@SessionAttribute("zsmenuList") List<Menu> zsmenuList) {
        return zsmenuList;
    }
    /**
     * 财务管理二级菜单
     * @param cwmenuList
     * @return
     */
    @RequestMapping("/cwmenuList")
    @ResponseBody
    public List<Menu> cwmenuList(@SessionAttribute("cwmenuList") List<Menu> cwmenuList) {
        return cwmenuList;
    }
    /**
     * 教学管理二级菜单
     * @param jxmenuList
     * @return
     */
    @RequestMapping("/jxmenuList")
    @ResponseBody
    public List<Menu> jxmenuList(@SessionAttribute("jxmenuList") List<Menu> jxmenuList) {
        return jxmenuList;
    }
    /**
     * 就业管理二级菜单
     * @param jymenuList
     * @return
     */
    @RequestMapping("/jymenuList")
    @ResponseBody
    public List<Menu> jymenuList(@SessionAttribute("jymenuList") List<Menu> jymenuList) {
        return jymenuList;
    }
    /**
     * 关系维护二级菜单
     * @param gxmenuList
     * @return
     */
    @RequestMapping("/gxmenuList")
    @ResponseBody
    public List<Menu> gxmenuList(@SessionAttribute("gxmenuList") List<Menu> gxmenuList) {
        return gxmenuList;
    }
	/**
	 * 注销
	 */
	@GetMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/login";
	}
	

	@RequestMapping("/recruit_students")
	public void recruit_students(Model model) {
		Iterable<Emp> emp = stuMapper.emplist();
		model.addAttribute("emp", emp);
	}
	/**
	 * 前台主界面
	 * @param session
	 * @param model
	 */
	@RequestMapping("/front_page")
	public void frontPage(HttpSession session,Model model,Project project) {
		//获取登录用户session
		User user = (User) session.getAttribute("user");
		//获取用户身份id
		Long cid = claMapper.returnclassid(user.getStudent().getId());
		//通过用户身份id查询班级
		Classes cla = claDao.findOne(cid);
		StudnetClassShadow s = new StudnetClassShadow();
		s.setShadowId(cla.getId());
		s.setShadowName(cla.getClassName());
		StudnetClassShadow scs = claMapper.selectstuid(s);
		//存进model作用域
		model.addAttribute("cla", cla);
		model.addAttribute("classMonitor", scs);
		Student stu = stuDao.findOne(user.getStudent().getId());
		//获取所有作业
		List<WorkJinDu> jd = jdMapper.selectAllwork(user.getStudent().getId());
		model.addAttribute("nowjd", jd);
		
		//根据学员ID查询已完成项目名称
		List<Map<String,Object>> leader=projectMapper.FindProjectByStudentId(user.getStudent().getId());
		model.addAttribute("count", leader.size());
		model.addAttribute("leader", leader);
		
		List<Map<String, Object>> best=teaMapper.selectBest();
		model.addAttribute("best", best);
		
		//根据学员ID查询未完成项目名称
		List<Map<String,Object>>  leaderNo=projectMapper.FindProjectByStudentIdNo(user.getStudent().getId());		
		model.addAttribute("countNo", leaderNo.size());
		model.addAttribute("leaderNo", leaderNo);
	
		//正在进行中的作业
		List<WorkJinDu> handjd = new ArrayList<>();
		//已批阅、过期、等待批阅的作业
		List<WorkJinDu> Markjd = new ArrayList<>();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String etime = df.format(new Date());
		Date edate = null;
		try {
			edate = df.parse(etime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date date = null; 
		
		for(int i=0;i <jd.size();i++) {
			try {
				date=df.parse(jd.get(i).getWork().getEndTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}  
			if(edate.getDate() > date.getDate() && ("未做".equals(jd.get(i).getState()) || "进行中".equals(jd.get(i).getState()))) {
				WorkJinDu  element = jd.get(i);
				element.setState("已过期");
			   	jd.set(i, element);
			   	jdDao.save(element);
			}
		}
		//根据作业的状态和时间
		for(int i=0;i <jd.size();i++) {
			if("已批阅".equals(jd.get(i).getState()) || "已过期".equals(jd.get(i).getState()) || "等待批改".equals(jd.get(i).getState())) {
				Markjd.add(jd.get(i));
			}else if("未做".equals(jd.get(i).getState()) || "进行中".equals(jd.get(i).getState())){
				handjd.add(jd.get(i));
			}
		}
		//统计图数据
		List<Integer> count = new ArrayList<>(16);
		String[] address = {"株洲","上海","广州","其他"};
		for(int j=0;j<address.length;j++) {
			Integer t = teaMapper.selectCity("%"+address[j]+"%");
			count.add(t);
		}
		model.addAttribute("citytj", count);
		model.addAttribute("handjd", handjd);
		model.addAttribute("Markjd", Markjd);
	}
	@RequestMapping("/front_homework")
	public void front_homework(HttpSession session,Model model) {
				//获取登录用户session
				User user = (User) session.getAttribute("user");
				//获取用户身份id
				Long cid = claMapper.returnclassid(user.getStudent().getId());
				//通过用户身份id查询班级
				Classes cla = claDao.findOne(cid);
				//存进model作用域
				model.addAttribute("cla", cla);
				Student stu = stuDao.findOne(user.getStudent().getId());
				//获取所有作业
				List<WorkJinDu> jd = jdMapper.selectAllwork(user.getStudent().getId());
				model.addAttribute("nowjd", jd);
				//已批阅、过期的作业
				List<WorkJinDu> myMarkjd = new ArrayList<>();
				List<WorkJinDu> myoverjd = new ArrayList<>();
				//保存成绩
				//优
				List<Double> excellent = new ArrayList<>();
				//良
				List<Double> good = new ArrayList<>();
				//中
				List<Double> middle = new ArrayList<>();
				//差
				List<Double> difference = new ArrayList<>();
				//不及格
				List<Double> fail = new ArrayList<>();
				//根据作业的状态和时间
				for(int i=0;i <jd.size();i++) {
					if("已批阅".equals(jd.get(i).getState()) ) {
						myMarkjd.add(jd.get(i));
						if(jd.get(i).getAllScore()<=100 && jd.get(i).getAllScore()>=90) {
							excellent.add(jd.get(i).getAllScore());
						}else if(jd.get(i).getAllScore()<=89 && jd.get(i).getAllScore()>=80) {
							good.add(jd.get(i).getAllScore());
						}else if(jd.get(i).getAllScore()<=79 && jd.get(i).getAllScore()>=70) {
							middle.add(jd.get(i).getAllScore());
						}else if(jd.get(i).getAllScore()<=69 && jd.get(i).getAllScore()>=60) {
							difference.add(jd.get(i).getAllScore());
						}else if (jd.get(i).getAllScore()<=59){
							fail.add(jd.get(i).getAllScore());
						}
						
					}else if("已过期".equals(jd.get(i).getState())){
						myoverjd.add(jd.get(i));
					}
				}
				model.addAttribute("myMarkjd", myMarkjd);
				model.addAttribute("myoverjd", myoverjd);
				model.addAttribute("excellent", excellent);
				model.addAttribute("good", good);
				model.addAttribute("middle", middle);
				model.addAttribute("difference", difference);
				model.addAttribute("fail", fail);
	}
	@RequestMapping("/front_program")
	public void front_program(Model model,HttpSession session) {
		
		User user = (User) session.getAttribute("user");
		Long id=user.getStudent().getId();
		List<ProjectLeader> leader=projectMapper.isLeader(id);
		model.addAttribute("leader", leader.size());

		
	}
	
	/**
	 * 过了一周就添加项目计划，否则就编辑项目计划
	 * @param model
	 * @param session
	 */
	@RequestMapping("/right_program")
	public void right_program(Model model,HttpSession session) {
		User user = (User) session.getAttribute("user");
		//获取当前日期
		Calendar cal=Calendar.getInstance(); 
		cal.setTime(new Date());
		//截取周数
		int num = cal.get(Calendar.WEEK_OF_YEAR);
		int year=cal.getWeekYear();
		String date=year+"年"+"第"+num+"周";
		List<ProjectPlan> plans=projectMapper.findProjectPlanContentByIdAndZhou(date,user.getStudent().getId());
		model.addAttribute("plans", plans);
	}
	
	/**
	 * 根据id查看项目计划名
	 * @param id
	 * @param model
	 * @param session
	 */
	@RequestMapping({"/right_program_list","right_program_list/{id}/{name}"})
	public String right_program_list(@PathVariable(name = "id", required = false) Long id,@PathVariable(name = "name", required = false) String name,Model model,HttpSession session) {
		if(id==null) {
			User user = (User) session.getAttribute("user");
			List<ProjectPlan> plan=projectMapper.findProjectPlanById(user.getStudent().getId());	
			model.addAttribute("plan", plan);
		}else {
			List<ProjectPlan> plan=projectMapper.findProjectPlanById(id);
			model.addAttribute("plan", plan);
			model.addAttribute("name", name);
		}
	
		return "right_program_list";
	} 
	
	
	/**
	 * 根据id查看项目计划内容
	 * @param model
	 * @param session
	 */
	@RequestMapping("/right_program_plan_content")
	public void right_program_plan_content(Model model,HttpSession session) {
		
		User user = (User) session.getAttribute("user");
		//获取当前日期
		Calendar cal=Calendar.getInstance(); 
		cal.setTime(new Date());
		//截取周数
		int num = cal.get(Calendar.WEEK_OF_YEAR);
		int year=cal.getWeekYear();
		String date=year+"年"+"第"+num+"周";
		List<ProjectPlan> plans=projectMapper.findProjectPlanContentByIdAndZhou(date,user.getStudent().getId());	
		String name=user.getStudent().getName();
		model.addAttribute("name", name);
		model.addAttribute("plans", plans);
	} 
	/**
	 * 查看项目资料
	 * @param id
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("/right_resource/{pid}")
	public String right_resource(@PathVariable(name = "pid", required = false) Long id,Model model,HttpSession session) {
		Project p=projectDao.findOne(id);
		model.addAttribute("p", p);
		return "right_resource";
	} 
	
	@RequestMapping("/right_resource_list")
	public void right_resource_list(Model model,HttpSession session) {
			
	} 
	
	@RequestMapping("/right_project_member_list")
	public void right_project_member_list(Model model,HttpSession session) {
		User user = (User) session.getAttribute("user");
		Long id=user.getStudent().getId();
		List<ProjectLeader> leader=projectMapper.findAllLeaderByStuID(id);
		//根据组长id查询组员
		List<ProjectMembers> members=projectMapper.findAllMembers(leader.get(0).getId());
		model.addAttribute("member", members);
		
		
		
	} 
	
	@RequestMapping("/right_project_leader_list")
	public void right_project_leader_list(Model model,HttpSession session) {
		User user = (User) session.getAttribute("user");
		Long id=user.getStudent().getId();
		ProjectMembers member=projectMapper.findLeaderByMemberId(id);
		model.addAttribute("leader",member);
		
	}
	
	/**
	 * 查询学生所有的项目
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("/front_project_content")
	public String front_project_content(Model model,HttpSession session) {
		//获取session中的用户
		User user = (User) session.getAttribute("user");
		//根据学员ID查询已完成项目名称
		List<Map<String,Object>> me=projectMapper.FindProjectByStudentId(user.getStudent().getId());	
		for(int i=0;i<me.size();i++) {
			List<ProjectMembers> member=projectMapper.findAllMembers(Long.valueOf(me.get(i).get("tb_project_leader_id").toString()));
			model.addAttribute("member", member);
		}	
		model.addAttribute("leader", me);	
		
		//根据学员ID查询未完成项目名称g
		List<Map<String,Object>> leaderNo=projectMapper.FindProjectByStudentIdNo(user.getStudent().getId());
		for(int i=0;i<leaderNo.size();i++) {
			List<ProjectMembers> memberName=projectMapper.findAllMembers(Long.valueOf(leaderNo.get(i).get("tb_project_leader_id").toString()));
			model.addAttribute("members", memberName);
		}
		model.addAttribute("leaderNo", leaderNo);
		return "front_project_content";
		
	}
	
	@RequestMapping("/tuition")
	public void tuition(Model model) {
		//高中生的姓名
		List<Map<String, Object>> stu = hightMapper.selectAllStudentName();
		model.addAttribute("student", stu);
		//高中生的班级
		List<Map<String, Object>> cla = hightMapper.selectAllClassName();
		model.addAttribute("classes",cla);
		
		//大学生的姓名
		List<Map<String, Object>> stuu = uniMapper.selectAllStudentName();
		model.addAttribute("studentu", stuu);
		//大学生的班级
		List<Map<String, Object>> clau = uniMapper.selectAllClassName();
		model.addAttribute("classesu",clau);
		
	}

	@RequestMapping("/payrecord")
	public void payRecord(Model model,HttpSession session) {
		List<Map<String, Object>> stu = recordMapper.selectAllStudentNameByRecord();
		model.addAttribute("student", stu);
		
		Long empTeacher=((User)session.getAttribute("user")).getEmp().getId();
		Long count=recordMapper.findCount(empTeacher);
		model.addAttribute("count", count);
		
	}
	
	@RequestMapping("/payback")
	public void payBack(Model model) {
		List<Map<String, Object>> stu = backMapper.selectAllStudentName();
		model.addAttribute("student", stu);
	}
	

	
	@RequestMapping("/add_payrecord")
	public void addPayrecord() {}
	
	@RequestMapping("/add_payback")
	public void addPayback() {}

	@RequestMapping("/payrecord_detail")
	public void payrecordDetail() {}
	
	@RequestMapping("/add_payrecord_detail")
	public void addPayrecordDetail() {}

	
	
	@RequestMapping("/role")
	public void role() {}
	
	@RequestMapping("/user")
	public void user(User user, Model model) {
		Iterable<User> u = userDao.findAll();
		model.addAttribute("user",u);
	}
	
	@RequestMapping("/dept")
	public void dept(Dept dept,Model model) {
		List<Dept> d = deptMapper.list(dept);
		model.addAttribute("dept",d);
	}
	
	@RequestMapping("/emp")
	public String role(Role role, Model model) {
		List<Role> r = empMapper.listRole(role);
		model.addAttribute("role", r);
		return "/emp";
	}
	
	@RequestMapping("/add_role")
	public void addRole() {}
	
	@RequestMapping("/add_emp")
	public void addEmp() {}
	
	@RequestMapping("/resources")
	public void resources() {}
	

	@RequestMapping("/teaemployment")
	public void teaemployment(){}
	
	@RequestMapping("/addteaemployment")
	public void addteaemployment(){}
	
	@RequestMapping("/base")
	public void base(){}
	
	@RequestMapping("/player")
	public void player(){}
	
	@RequestMapping("/database")
	public void database(){}
	
	@RequestMapping("/resume")
	public void resume(){}
	
	@RequestMapping("/resume_details")
	public void resumeDetails(){}
	
	@Autowired
	private FollowUpDao followDao;
	
	@RequestMapping("/follow_uppage")
	public void follow_uppage(Model model) {
		Iterable<FollowUp> followup = followDao.findAll();
		model.addAttribute("follow", followup);
	}
	
	@RequestMapping("/sign_student")
	public void sign_student(Sign sign,Model model) {
		List<Sign> s = stuMapper.signList(sign);
		model.addAttribute("stu", s);
		Iterable<Emp> emp = stuMapper.emplist();
		model.addAttribute("emp", emp);
	}
	
	@RequestMapping("/transfer_page")
	public void transfer_page(Model model,HttpServletRequest request) {
		List<Transfer> s = stuMapper.transferName();
		model.addAttribute("stu", s);
		Iterable<Emp> emp = stuMapper.emplist();
		model.addAttribute("emp", emp);
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("user");
		int transfer = stuMapper.massageCount(u.getEmp().getId(), "进行中");
		model.addAttribute("t", transfer);
	}
	
	@RequestMapping("/report_from")
	public void tjtList(Model model){
		// 组装返回结果
		List<Map<String, Object>> list=new ArrayList<>();
		String[] gender = {"男","女"};
		String[] address = {"茶陵","醴陵","攸县","株洲","其他"};
		// 查询数据
		for(int j=0;j<gender.length;j++) {
			Map<String,Object> map = new HashMap<String, Object>();
			for(int i=0;i<address.length;i++) {
				Integer m = stuMapper.tjtList(gender[j],"%"+address[i]+"%");
				map.put(address[i], m);
			}
			list.add(map);
		}
		model.addAttribute("list", list);
	}
	
	
	@RequestMapping("/maintenance")
	public void maintenance(Model model) {
		List<Map<String, Object>> emp = mrm.selectAllEmp();
		model.addAttribute("emp", emp);
	}
	
	
	@RequestMapping("/new_technology")
	public void new_technology(Model model) {
		List<Map<String, Object>> technology = ntm.selectAllTechnologyNameAndType();
		model.addAttribute("technology",technology);
		
	}
	
	@RequestMapping("/bedroom")
	public void bedroom(Model model) {
		List<Map<String, Object>> bedroom = brm.selectAllBedroom();
		model.addAttribute("bedroom",bedroom);
	}
	
	
	@RequestMapping("/add_new_technology")
	public void add_new_technology() {}
	
	@RequestMapping("/add_maintenance_record")
	public void add_maintenance_record() {}
	
	@RequestMapping("/add_student_information")
	public void add_student_information() {}
	
	@RequestMapping("/student_information")
	public void student_information() {}
	

	@RequestMapping("/student_list")
	public void student_list(Model model){
		List<Student> student = sjm.selectAllStudent();
		model.addAttribute("student",student);
	}	
	
	@Autowired
	private ClassesMapper claMapper;
	
	@RequestMapping("/class_")
	public void classs(Emp emp,Model model) {
		List<Emp> jy = claMapper.listJy(emp);
		List<Emp> bzr = claMapper.listBzr(emp);
		List<StudnetClassShadow> cla = claMapper.listclass();
		List<StudnetClassShadow> gcla = claMapper.guticationclass();
		model.addAttribute("jy", jy);
		model.addAttribute("bzr", bzr);
		model.addAttribute("cla", cla);
		model.addAttribute("gcla", gcla);
	}
	@RequestMapping("/myrecruit_students")
	public void myrecruit_student() {}
	
	@RequestMapping("/distribution")
	public void distribution() {}
	
	@RequestMapping("/bedroom_details")
	public void bedroom_details() {}
	
	@RequestMapping("/recep_inter")
	public void recep_inter() {}
	
	@RequestMapping("/blacklist")
	public void blacklist() {}
	
	@RequestMapping("/recep_resume")
	public void recep_resume() {}
	
	@RequestMapping("/graduation")
	public void graduation() {}
	
	@RequestMapping("/myresume")
	public void myresume() {}
	
	@RequestMapping("/edit_resume")
	public void edit_resume() {}
	
	@RequestMapping("/resume_great")
	public void resume_great() {}
	
	@RequestMapping("/preview")
	public void preview() {}
	
	@RequestMapping("/resume_preview")
	public void resume_preview() {}
	
	@RequestMapping("/takework")
	public void takework() {}
	
	@RequestMapping("/statistics")
	public void statistics() {}
	
	@RequestMapping("/outline")
	public void outline(Model model) {
		List<DaGang> dg = claMapper.outlinelist();
		model.addAttribute("dg", dg);
	}
	
	@RequestMapping("/project_manage")
	public String projectManage(Model model,Classes cla) {
		List<Classes> cls =  claMapper.listClasses(cla);
		model.addAttribute("cls", cls);
		return "/project_manage";
	}
	

	@RequestMapping("/tongji")
	public String tongji(Model model) {
		List<Map<String, Object>> list = new ArrayList<>();
		String[] month = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
				"twelve" };
		String[] months = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
		for (int j = 0; j < month.length; j++) {
			Map<String, Object> m = new HashMap<>();
			Integer money = recordMapper.AllMoneyOfEveryMonth(months[j]);
			if(money==null) {
				money=0;
			}
			m.put(month[j], money);
			list.add(m);
		}
		model.addAttribute("list", list);
		return "tongji";
	}

	@RequestMapping("/project_plan")
	public String projectPlan(Model model,Classes cla) {
		List<Classes> cls =  claMapper.listClasses(cla);
		model.addAttribute("cls", cls);
		return "/project_plan";
	}
	
	@RequestMapping("/publish_exercises")
	public void publish_exercises(Classes c,Model model) {
		List<Classes> cla = claMapper.listgraduationClasses(c);
		List<DaGang> dg = claMapper.outlinelist();
		List<List<ZhangJie>> zjmap = new ArrayList<>();
		for(int i=0;i<dg.size();i++) {
			List<ZhangJie> zj =claMapper.allchaterlist(dg.get(i).getId());
			zjmap.add(i, zj);
		}
		model.addAttribute("cla", cla);
		model.addAttribute("dg", dg);
		model.addAttribute("zj", zjmap);
	}
	
	/**
	 * 前台修改密码
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/updatepassword")
	public String update_password(
			@PathVariable(name = "userid", required = false) Long id,
			Model model) {
		if (id != null) {
			//根据Id查询就业学员的反馈信息
			User user = userDao.findOne(id);
			//查询所有就业学员的姓名和id
			if (user != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				model.addAttribute("user", user);
			}
		}
		return "/update_password";
	}
	
	@PostMapping("update_password")
	public String updatepassword(HttpServletRequest request,HttpServletResponse response,Model model) {
					String oldpassword=request.getParameter("oldpassword");
					String newpassword=request.getParameter("newpassword");
					String password=request.getParameter("password");
					HttpSession session = request.getSession();
					User u = (User) session.getAttribute("user");
					String error="";
					if(oldpassword.equals("") ||newpassword.equals("")||password.equals("") ){
						error="不能为空！";
						model.addAttribute("error", error);
						return "/update_password";
					} else if(newpassword.equals(u.getPassword())) {
						error="新密码与旧密码一致，不能修改！";
						model.addAttribute("error", error);
						return "/update_password";
					}else if(!password.equals(newpassword)) {
						error="确认密码错误！";
						model.addAttribute("error", error);
						return "/update_password";
					}else if(!oldpassword.equals(u.getPassword())  ) {
						error="原始密码输入错误！";
						model.addAttribute("error", error);
						return "/update_password";
					}else {
						empMapper.updatePassword(password,u.getId());
						return "/login";
					}
		}

	
	@RequestMapping("/marking_work")
	public void marking_work(HttpSession session,Model model) {
		//获取所有班级id
		List<Long> claids = claMapper.selectclaid();
		//获取所有关联班级作业id
		List<Long> workids = claMapper.selectwordid();
		//保存所有班级的集合
		List<Classes> clas = new ArrayList<>();
		//保存所有作业的集合
		List<Chuti> cts = new ArrayList<>();
		//保存未完成的作业的集合
		List<Integer> count = new ArrayList<>();
		
		//保存作业没有过期的班级
		List<Classes> newclas = new ArrayList<>();
		//保存作业过期的班级
		List<Classes> pastclas = new ArrayList<>();
		//保存没有过期的作业
		List<Chuti> newcts = new ArrayList<>();
		//保存过期的作业
		List<Chuti> pastcts = new ArrayList<>();
		for(int i = 0;i<claids.size();i++) {
			Classes cla = claDao.findOne(claids.get(i));
			Chuti ct = ctDao.findOne(workids.get(i));
			clas.add(cla);
			cts.add(ct);
		}
		int sum = 0;
		for(int i=0;i<clas.size();i++) {
			//通过班级id获取关联这个班的学生
			List<Long> stuids = claMapper.returnAllclastuid(clas.get(i).getId());
			for(int j=0;j<stuids.size();j++){
				//获取学生对象
				Student stu = stuDao.findOne(stuids.get(j));
				//获取作业对象
				Chuti ct=cts.get(i);
				WorkJinDu wjd = new WorkJinDu();
				wjd.setStudent(stu);
				wjd.setWork(ct);
				Long jd = jdMapper.selectWorkjd(wjd);
				if(jd != null) {
					//获取作业进度对象
					WorkJinDu jds = jdDao.findOne(jd);
					if("等待批改".equals(jds.getState())) {
						sum++;
					}
				}
			}
			count.add(sum);
			sum = 0;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		Date endtime = null;
		for(int i=0;i<cts.size();i++) {
			try {
				endtime = sdf.parse(cts.get(i).getEndTime());
			} catch (ParseException e) {
				log.error("日期转换异常",e);
			}
			if(date.getYear() >= endtime.getYear() &&date.getMonth() >= endtime.getMonth() && date.getDate() > endtime.getDate()) {
				pastclas.add(clas.get(i));
				pastcts.add(cts.get(i));
			}else {
				newclas.add(clas.get(i));
				newcts.add(cts.get(i));
			}
		}
		//获取提交作业日志的学生班级集合
		List<Classes> classes = new ArrayList<>();
		List<WorkJournal> jl = jdMapper.selectworkjl();
		for(int i=0;i<jl.size();i++) {
			Long cid= claMapper.selectWorkJournalid(jl.get(i).getStudent().getId());
			Classes cla = claDao.findOne(cid);
			classes.add(cla);
		}
		model.addAttribute("clas",clas);
		model.addAttribute("cts",cts);
		model.addAttribute("newclas",newclas);
		model.addAttribute("newcts",newcts);
		model.addAttribute("pastclas",pastclas);
		model.addAttribute("pastcts",pastcts);
		model.addAttribute("count",count);
		model.addAttribute("jl",jl);
		model.addAttribute("classes",classes);
		
		//获取所有作业
		
	}
	
}
